<!--
**********************************************************
* @Author: 张小平
* @Date:   2024-05-01
* @Email:  qqshuqian@163.com
* @Last modified: 最后修改日期
* @Description: 文件描述
**********************************************************
-->
<template>
  <div class="user">
    <basic-container>

      <avue-crud
         v-model="form"
         v-model:search="searchForm"
        v-model:page="page"
         :option="option"
         :data="tableData"
         :permission="getPermission"
        v-bind="bindVal"
        v-on="onEvent"
        :before-open="beforeOpen"
      >
        <template #menu-left="{size}">

          <el-button type="primary" @click="importShowHandle()">导入</el-button>
          <el-button type="primary" @click="exportHandle()">导出</el-button>

        </template>


        <template #menu="{size,row,index}">
          <el-button v-if="permissions.sys_user_psw_reset_post" link type="primary"  size="small" icon="el-icon-lock" @click="resetPassword(row,index)">重置密码</el-button>
          <el-button v-if="permissions.sys_user_setorg3_post" link type="primary"  size="small" icon="el-icon-lock" @click="setRole(row,index)">机构角色管理</el-button>
<!--          <el-button v-if="permissions.sys_user_setorg3_post" link type="primary"  size="small" icon="el-icon-lock" @click="addRole(row,index)">新增机构角色</el-button>-->
          <el-button v-if="permissions.sys_user_temppwd_post" link type="primary"  size="small" icon="el-icon-lock" @click="tempPassword(row,index)">临时密码</el-button>

        </template>

      </avue-crud>
    </basic-container>

    <el-dialog  v-model="dialogFormVisible" title="组织角色管理" width="60%">
      <avue-crud
        ref="itemCrud"
        :data="tableItemData"
        :option="tableItemOption"
         @row-del="handleItemDel"
    >
        <template #menu-left="{}">
          <el-button
                     icon="el-icon-plus"
                     @click="addRole">新增</el-button>
         </template>
        <template #menu="{row,index,size}">
        <el-button
                   :size="size"
                   icon="el-icon-edit"
                   @click="handleItemUpdate(row,index)">编辑</el-button>
        </template>
      </avue-crud>
    </el-dialog>

    <import-upload :base-url="pageConfig.url" :model-name="pageConfig.name" v-model="importDialogVisible"  ></import-upload>


<!--    <el-dialog header="导入" width="40%" v-model="importDialogVisible" :modal-append-to-body="false">-->
<!--      <div>-->
<!--        <span style="color:red;">* 导入说明：</span>-->
<!--         <p>1.下载的模板,非必要请不要修改，特别是第1，2行</p>-->
<!--         <p>2.支持同步、异步导入。同步：直到执行完毕才返回结果，数据量小，执行时间短可以使用次方法，如果时间长会导致超时异常-->
<!--             ；异步：没有执行完就会返回，数据量大或者时间长建议使用次方法，执行结果会以消息通知的方式提示</p>-->
<!--      </div>-->
<!--      <el-button style="margin:10px 0 10px 0;" type="primary"  icon="el-icon-download" @click="downExcel">下载模版</el-button>-->
<!--      <avue-switch-->
<!--              active-color="#13ce66"-->
<!--              inactive-color="#ff4949"-->
<!--              v-model="uploadCpnfig.type"-->
<!--              :dic="uploadCpnfig.dic"></avue-switch>-->
<!--      <el-upload-->
<!--              :http-request="upAndDowndoad"-->
<!--              action="#"-->
<!--              :on-change="onChange"-->
<!--              :auto-upload="true"-->
<!--              :show-file-list="true"-->
<!--              :on-success="importHandleSuccess"-->
<!--              :on-error="importHandleError"-->
<!--              accept=".xls, .xlsx"-->
<!--      >-->

<!--        <div  style="width:100%;margin-top: 10px">-->
<!--          <el-button type="primary" icon="el-icon-upload">导入模版</el-button>-->
<!--        </div>-->
<!--      </el-upload>-->
<!--    </el-dialog>-->

    <table-form v-model="userForm" v-if="addOrUpdateVisible" ref="addOrUpdate" ></table-form>
  </div>
</template>

<script>
  import crud from "@/framework-core/mixins/crud";
import TableForm from "./user-role-form.vue";
import {resetPassword,showOrgRole,setOrgRole,delOrgRole,tempPassword} from "@/framework-admin/api/admin/user";
import {tableOption,tableItemOption} from "./user";
  import website  from '@/framework-core/config/website';
  import ImportUpload from "@/framework-core/components/upload/index.vue";

export default {
  name: "SysUser",
  mixins: [crud],
  components: {TableForm,ImportUpload},
  data() {
    return {
      pageConfig:{"url":"sys/user", name: "用户"},
      option: tableOption,
      //表格
      rowData: {},
      role: [],
      roleData: [],
      defaultProps: {
        label: "name",
        value: "id"
      },
      currentUserId:"",
      //字典项
      itemForm: {},
      dialogFormVisible: false,
      tableItemOption: tableItemOption,
      tableItemData: [],
      itemPage: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: website.pageSize // 每页显示多少条
      },
      userForm:{},
      addOrUpdateVisible:false
    };
  },
  computed: {
    ...mapState({
       permissions: (state) =>state.user.permission
    }),
    ...mapGetters([
      "userInfo",
    ])
  },
  created() {
    this.option.menu =true;
    this.option.editBtn= true;
    this.option.delBtn= true;
    this.option.addBtn= true;
    this.init();
  },
  methods: {
    init() {

    },
    getPermission:function(key, row, index) {

      return this.getCommPermission(key, row, index);
    },
    saveBefor(rowData){
      //rowData.orgId = 141
    },
    beforeOpen(show, type,) {
      //this.$message.success("22");
      // 通过用户ID查询角色列表
      if(type=="edit"){
        // getRoleIdsByUserId(this.rowData.id).then(response => {
        //   this.$set(this.$refs.crud.tableForm,"roleIds",response.data.data)
        // });

      }

      show();
    },

    resetPassword(row){
      resetPassword(row.id).then(res=>{
        if(res.data.success){
          this.$message.success("重置成功");
        }else{
          this.$message.error(res.data.message);

        }
      })
    },
    tempPassword(row){
      tempPassword(row.id).then(res=>{
        if(res.data.success){
          this.$message.success("临时密码5分钟内有效："+res.data.data.data);
        }else{
          this.$message.error(res.data.message);

        }
      })
    },

    setRole(row){
      this.currentUserId = row.id;
      this.getUserOrgRole(row.id);
    },
    //======获取用户的机构及角色=====
    getUserOrgRole(userId) {
      this.dialogFormVisible = true;
      showOrgRole(userId
      ).then(response => {
        this.tableItemData = response.data.data.data||[];
      });
    },

    // handleItemSave: function(row, done) {
    //   setOrgRole(row).then(() => {
    //     this.$message.success("添加成功");
    //     this.getUserOrgRole(this.currentUserId);
    //     done();
    //   });
    //   //this.handleItem({},index,done);
    // },
    // handleItemUpdate: function(row, index, done) {
    //   var $that = this;
    //   setOrgRole(row).then(() => {
    //     $that.$message.success("修改成功");
    //     this.getUserOrgRole(this.currentUserId);
    //     done();
    //   });
    //   //this.handleItem(row,index,done);
    // },

      addRole: function(row, index, done) {
      this.addOrUpdateVisible = true;
        this.userForm = {};
      this.$nextTick(() => {
        //this.getUserOrgRole(this.currentUserId);
        this.$refs.addOrUpdate.init(false,row,this.currentUserId);
      });
    },
    handleItemUpdate (row, index, done) {
      this.addOrUpdateVisible = true;
      this.userForm = row;
      this.$nextTick(() => {
        //this.getUserOrgRole(this.currentUserId);
        this.$refs.addOrUpdate.init(true,row,this.currentUserId);
      });
    },
    handleItemDel: function(row) {
      this.$confirm('是否确认删除数据为的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delOrgRole(row.id);
        })
        .then(() => {
          this.getUserOrgRole(this.currentUserId);
          this.$message.success("删除成功");
        })
        .catch(function() {});
    },
    // itemBeforeOpen: function(done, type) {
    //   if(type=="add"){
    //    // this.$set(this.$refs.itemCrud.tableForm,"currentUserId",this.currentUserId)
    //    // this.$set(this.$refs['itemCrud'].tableForm,"dictType",this.dictType)
    //   }
    //   if(type=="edit"){
    //
    //   }
    //   //done();
    // },
    // itemBeforeClose: function(done, type) {
    //   done();
    // },
  }
};
</script>
<style lang="scss">

</style>
